package com.lemon.smartsafety.ui.fragment.news;

import android.os.Bundle;
import android.view.Gravity;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.news.NewsSearchEvent;
import com.lemon.smartsafety.entity.news.ArticleInfoVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.service.NewsInfoServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.news.NewsAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.thefinestartist.finestwebview.FinestWebView;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/4/7.
 */
public class NewsContent extends BasePullToRefreshFragment<ArticleInfoVo, List<ArticleInfoVo>> implements RecyclerTClickListener<ArticleInfoVo> {
    private LoggerHelper loggerHelper = new LoggerHelper(NewsContent.class);

    private String mKeyWord      = "";
    private String mNewsColumnId = "";


    public static NewsContent newInstance(String columnId) {
        Bundle args = new Bundle();
        args.putString(BundleKey.NEWS_COLUMN_ID, columnId);
        NewsContent pageFragment = new NewsContent();
        pageFragment.setArguments(args);
        return pageFragment;
    }


    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        if (getArguments() != null) {
            mKeyWord = getArguments().getString(BundleKey.KEY_WORD);
            mNewsColumnId = getArguments().getString(BundleKey.NEWS_COLUMN_ID);
        }
        mKeyWord = StringUtils.nullValue(mKeyWord);
        mNewsColumnId = StringUtils.nullValue(mNewsColumnId);
    }

    @Override
    public boolean goRefreshOnCreate() {
        if (mDataList.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public LemonRcvAdapter<ArticleInfoVo> getLemonAdapter() {
        return new NewsAdapter(mDataList, this);
    }


    @Override
    public boolean getUserVisibleHint() {
        return super.getUserVisibleHint();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && mDataList.size() == 0 && mPullToRefreshRecyclerView != null) {
            if (mPullToRefreshRecyclerView == null) {
                onPullRefresh();
            } else {
                mPullToRefreshRecyclerView.beginRefreshing();
            }
        } else if (isVisibleToUser) {
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Subscribe
    public void onEventMainThread(NewsSearchEvent event) {//搜索事件
        if ((NewsSearchEvent.class.getName() + mNewsColumnId).equals(event.getTag())) {
            mNewsColumnId = event.getColumnId();
            mKeyWord = event.getData();
            mPullToRefreshRecyclerView.beginRefreshing();
        }
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_NEWS_LIST + mNewsColumnId;
    }

    @Override
    public void onLoadMore() {
        NewsInfoServices.init().getNewsList(RefreshType.LOAD_MORE, pageSize, currentPage, mNewsColumnId, mKeyWord);
    }

    @Override
    public void onPullRefresh() {
        NewsInfoServices.init().getNewsList(RefreshType.REFRESH, pageSize, currentPage, mNewsColumnId, mKeyWord);
    }



    @Override
    public void onItemClick(ArticleInfoVo data, int position) {

        String url = new SharedPreferencesServer(getContext()).getRequestUrl(UrlInfo.ARTICLE_DETAIL);
        url += "?rp=" + Constants.RP_PASSWORD + "&article_id=" + StringUtils.nullValue(data.getArticleId()) + "&page_size=1&current_page=1";

        new FinestWebView.Builder(getActivity()).theme(R.style.FinestWebViewTheme)
                                                .titleDefault("公告新闻")
                                                .showUrl(false)
                                                .statusBarColorRes(R.color.colorPrimaryDark)
                                                .toolbarColorRes(R.color.colorPrimary)
                                                .titleColorRes(R.color.white)
                                                .urlColorRes(R.color.colorPrimaryLight)
                                                .iconDefaultColorRes(R.color.finestWhite)
                                                .progressBarColorRes(R.color.finestWhite)
                                                .showSwipeRefreshLayout(true)
                                                .swipeRefreshColorRes(R.color.colorPrimaryDark)
                                                .menuSelector(R.drawable.selector_light_theme)
                                                .menuTextGravity(Gravity.CENTER)
                                                .menuTextPaddingRightRes(R.dimen.defaultMenuTextPaddingLeft)
                                                .dividerHeight(0)
                                                .gradientDivider(false)
                                                .setCustomAnimations(R.anim.slide_up, R.anim.hold, R.anim.hold, R.anim.slide_down)
                                                .showMenuShareVia(false)
                                                .stringResCopiedToClipboard(R.string.web_view_copied)
                                                .stringResCopyLink(R.string.web_view_copy)
                                                .stringResOpenWith(R.string.web_view_open)
                                                .show(url);
    }
}
